package com.ssh.manager.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ssh.common.util.Page;
import com.ssh.dao.IDao;
import com.ssh.entity.Product;
import com.ssh.entity.SuperMarketProduct;
import com.ssh.manager.SuperMarketProductManager;

@Service
public class SuperMarketProductManagerImpl implements SuperMarketProductManager {
	@Resource
	private IDao daoImpl;
	
	@Override
	public SuperMarketProduct getSuperMarketProductById(Long id) {
		return daoImpl.getDomain(SuperMarketProduct.class, id);
	}

	@Override
	public List<SuperMarketProduct> findByMid_Bid(Long marketId, Long brandId) {
		String sql = "from SuperMarketProduct where del!=1 and superMarket.id="+marketId;
		sql += " and product.brand.id="+brandId;
		List<SuperMarketProduct> smpList = (List<SuperMarketProduct>)daoImpl.findListBySql(sql, new Object[]{}, false, true, false, 0, -1);
		return smpList;
	}

	@Override
	public List<SuperMarketProduct> findBysmId_brandId(Long marketId,
			String[] brandIds) {
		String sql = "from SuperMarketProduct where del!=1 and superMarket.id="+marketId;
		sql += " and product.id in(select id from Product where brand.id in(";
		for (int i = 0; i < brandIds.length; i++) {
			if (i==0) {
				sql += brandIds[i];
			}else {
				sql += ","+brandIds[i];
			}
		}
		sql += "))";
		List<SuperMarketProduct> smpList = (List<SuperMarketProduct>)daoImpl.findListBySql(sql, new Object[]{}, false, true, false, 0, -1);
		return smpList;
	}

	@Override
	public SuperMarketProduct saveOrUpdateEntity(
			SuperMarketProduct superMarketProduct) {
		return daoImpl.saveOrUpdateDomain(superMarketProduct);
	}

	@Override
	public void findMarketProductListByPage(Map conditionMap, Page page,
			String isDesc) {
		String sql = "select count(*) from SuperMarketProduct where del!=1";
		String findSql = "from SuperMarketProduct where del!=1";
		if (conditionMap.get("marketId")!=null && !conditionMap.get("marketId").equals("")) {
			sql += " and superMarket.id="+conditionMap.get("marketId");
			findSql += " and superMarket.id="+conditionMap.get("marketId");
		}
		if (conditionMap.get("productName")!=null && !conditionMap.get("productName").equals("")) {
			sql += " and product.name like '%"+conditionMap.get("productName")+"%'";
			findSql += " and product.name like '%"+conditionMap.get("productName")+"%'";
		}
		//sql = sql.replaceFirst("and", "where");
		//findSql = findSql.replaceFirst("and", "where");
		
		int productSum = daoImpl.countAll(sql, null);
		page.setTotalCount(new Long(productSum));
		page.countTotalPageNum();
		
		findSql += " order by createDate "+isDesc;
		List<SuperMarketProduct> superMarketProducts = (List<SuperMarketProduct>)daoImpl.findListBySql(findSql, new Object[]{}, false, true, false, page.getNumberPerPage()*(page.getCurPageNum()-1), page.getNumberPerPage());
		page.setPageData(superMarketProducts);
	}
}
